package com.ztf0337.mycurriculumdesign.service;

import android.content.Context;
import android.util.Log;
import android.widget.LinearLayout;

import com.ztf0337.mycurriculumdesign.dao.OrderDao;
import com.ztf0337.mycurriculumdesign.database.AppDatabase;
import com.ztf0337.mycurriculumdesign.entity.Order;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class OrderService {

    private final OrderDao orderDao;
    public OrderService(Context context) {
        AppDatabase db = AppDatabase.getInstance(context);
        this.orderDao = db.orderDao();
    }

    public long insert(Order order) {
        return orderDao.insert(order);
    }

    public void delete(Order order) {
        orderDao.delete(order);
    }

    public Order getOrderById(Long id) {
        return orderDao.getOrderById(id);
    }

    public void update(Order order) {
        orderDao.update(order);
    }

    public List<Order> getOrdersByUserId(Long id){
        return orderDao.getOrdersByUserId(id);
    }

    public List<Order> searchOrders(String content, Long userId) {
        List<Order> list1 = orderDao.searchOrderByOrderNumber(content, userId);
        List<Order> list2 = orderDao.searchOrderBySpaceNumber(content, userId);

        Set<Order> orderSet = new LinkedHashSet<>();
        orderSet.addAll(list1);
        orderSet.addAll(list2);

        return new ArrayList<>(orderSet);
    }
    public List<Order> getAll() {
        return orderDao.getAll();
    }

    public List<Order> searchAllOrders(String content) {
        List<Order> list1 = orderDao.searchAllOrderByOrderNumber(content);
        List<Order> list2 = orderDao.searchAllOrderBySpaceNumber(content);

        Set<Order> orderSet = new LinkedHashSet<>();
        orderSet.addAll(list1);
        orderSet.addAll(list2);

        return new ArrayList<>(orderSet);
    }

    public List<Order> filterData(int status, int type, int date, Long userId) {
        List<Order> orderList = orderDao.getOrdersByUserId(userId);
        if (orderList.isEmpty()) {
            return orderList;
        }

        if (status == 0 && type == 0 && date == 0) {
            return orderList;
        }

        Boolean s = null;
        if (status == 1) {
            s = true;
        } else if (status == 2) {
            s = false;
        }

        String t = null;
        if (type == 1) {
            t = "ELECTRIC_SMALL_CAR";
        } else if (type == 2) {
            t = "ELECTRIC_MEDIUM_CAR";
        } else if (type == 3) {
            t = "ELECTRIC_LARGE_CAR";
        } else if (type == 4) {
            t = "GASOLINE_SMALL_CAR";
        } else if (type == 5) {
            t = "GASOLINE_MEDIUM_CAR";
        } else if (type == 6) {
            t = "GASOLINE_LARGE_CAR";
        }

        Boolean d = null;
        if (date == 1) {
            d = false;
        } else if (date == 2) {
            d = true;
        }

        List<Order> o1;
        if (s != null) {
            o1 = new ArrayList<>();
            for (Order o : orderList) {
                if (o.getTransactionStatus().equals(s)) {
                    o1.add(o);
                }
            }
        } else {
            o1 = new ArrayList<>(orderList);
        }

        List<Order> o2;
        if (t != null) {
            o2 = new ArrayList<>();
            for (Order o : o1) {
                if (o.getParkType().equals(t)) {
                    o2.add(o);
                }
            }
        } else {
            o2 = new ArrayList<>(o1);
        }

        for (Order o : o2) {
            Log.d("筛选测试","前订单号 "+o.getOrderNumber());
        }
        Log.d("反转测试","d "+d);
        if (d != null && !d) {
            Log.d("反转测试", "执行反转");
            Collections.reverse(o2);
        }

        for (Order o : o2) {
            Log.d("筛选测试","后订单号 "+o.getOrderNumber());
        }
        return o2;

    }

}
